﻿__ID =0;//跟节点ID
Jes.ext({
	Aclrole : {//ajax
		init : function(){
			if(!Jes.usSess()) return;
			initTabs('tabView_1',Array('角色管理'),0,'100%',90,Array(false));
			this.query();
		}
		,query : function(){//父节点
			var html = '<ul id="ajax_tree" class="dhtmlgoodies_tree">';
			Jes.queryO('Aclrole',{par:{'id':__ID}},function(da){
				html += '<li><a href="javascript:void(0);" id="'+da.id+ '" onclick="Jes.Aclrole.node(\''+da.id+'\');return false;">'+da.rname+'</a>'
				+'<ul><li parentId="'+da.id+'" class="loading.gif"><a href="javascript:void(0);">Loading...</a></li></ul></li>';
				Jes.html('org_tree',html+'</ul>');
				initTree();//初始化树
				initContextMenu();//初始化菜单
			});
		}
		//子节点
		,queryChildNodes : function(ulId,pId){
			DWRUtil.useLoadingMessage('正在读取数据...');
			var html='';
			Jes.query('aclrolebypid_query_sql@acl',{lo:0,par:{pid:pId}},function(da){
				Jes.each(da,function(i){
					html+='<li><a href="javascript:void(0);" id="'+da[i].id+'" onclick="Jes.Aclrole.node(\''+da[i].id+'\');return false;">'+da[i].rname+'</a>';
					if(da[i].isnext=='1') html+='<ul><li parentId="'+da[i].id+'" class="loading.gif"><a href="javascript:void(0);">Loading</a></li></ul>';
					html+='</li>';
				});
				Jes.html(ulId,html);
				if(html==''){
					Jes(ulId).parentNode.getElementsByTagName('IMG')[0].style.visibility='hidden';
					Jes.hide(ulId);
				}
				parseSubItems(ulId,pId);
			});
		}
		,open : function(type,id){
			var args= arguments,p=Jes.vals(['us.rid','us.rname'],'us');
			switch(type){
				case 'user':
						p.id=id;
						_newTab(id?'用户编辑':'关联用户',(id?'acluserop.html':'acluserquery.html').J(p));
					break;
			}
		}
		,opInit : function(type,p2id,pName){
			if(!Jes.usSess()) return;
			if(type=='add'){
				setTab('0','角色新增');
				Jes.Aclrole.html(Jes.html('div_org_op'));
				Jes.vals({'pid':p2id,'pname':pName});
				Jes.Aclrole.menuTree.init();//显示菜单树
			}else if(type=='edit'){
				if(p2id==__ID){alert('根节点不能编辑！');return;}
				setTab('0','角色修改');
				Jes.Aclrole.html(Jes.html('div_org_op'));
				Jes.queryO('Aclrole',{par:{id:p2id}});
				Jes.val('pname',pName);
				//显示菜单树
				Jes.Aclrole.menuTree.init();
			}else{//del
				Jes.sql('aclrole_del_sql@acl',{ids:p2id},function(da){
					alert('删除成功!');
					delTab('关联用户');
					delTab('用户编辑');
					Jes.Aclrole.html('');
				});
			}
		}
		,menuTree : {
			init : function(){
				DWRUtil.useLoadingMessage('正在读取数据...');
				Jes.rpc('getMenuTree@acl',{lo:0,par:{drag:'1',all:'1',sync:'1',type:'chk',upD:'1'}},function(da){
					Jes.html('menu_tree',da);
					treeObj = new JSDragDropTree();
					//treeObj.fn = 'Jes.Fmenu.edit';
					treeObj.setTreeId('dhtml_tree');
					treeObj.setMaximumDepth(10);
					treeObj.setMessageMaximumDepthReached('超过最多级别'); // If you want to show a message when maximum depth is reached, i.e. on drop.
					treeObj.initTree();
					treeObj.expandAll();
					//treeObj.expand(true);
					Jes.Aclrole.queryMenuByRo();
				});
			}
		}
		,queryMenuByRo : function(){
			Jes.query('menuidbyrid_query_sql@acl',function(da){
				var mids= Jes.names('tree.menuid');
				Jes.each(mids,function(i){//优化
					Jes.each(da,function(j){
						if(mids[i].value==da[j].id){
							mids[i].checked=true;
							return false;
						} 
					});
				});
			});
		}
		//点击节点
		,node : function(id){
			setTab('0','角色管理');
			delTab('关联用户');
			delTab('用户编辑');
			var name =Jes.html(id);
			if(id =='0'){this.html('');return;}
			Jes.Aclrole.html(Jes.html('div_org2us'));
			Jes.vals({'us.rid':id,'us.rname':name,'o_tit':name});
			Jes.Aclrole.queryUs();
		}
		,queryUs : function(){
			Jes.page('acluserbyrole_query_sql@acl',{lo:0});
		}
		,delUs : function(){
			var ids = Jes.val('id');
			if(Jes.isN(ids)){alert('至少选择一项!');return;}
			if(!confirm('你确定移除用户吗？')) return;
			Jes.sql('aclroleuser_del_sql@acl',{ids:ids,rid:Jes.v('us.rid')});
		}
		,save : function(){
			var id=Jes.v('id');
			Jes.save('Aclrole@acl',function(da){
				alert('保存成功!');
				if(Jes.isN(id)){
					loadNodeByParentId(da.pid);//重新读取父节点
				}else{
					loadNodeById(da.id,da.rname);//同时修改树的名称
				}
				Jes.Aclrole.html('');
			});
		}
		,html : function(html){
			Jes.html('div_role',html);
		}
	}
});
//tree固定方法
loadTreeChildNodes=Jes.Aclrole.queryChildNodes;
opTreeNode=Jes.Aclrole.opInit;